function [Q,Qs,R2] = gmm_time(x,pi_bar,pi_lim,f_d,g_d,bts,mdl,tot_u,k,ki)

Qs = zeros(tot_u,1);
R2 = zeros(tot_u,3);
td = (k-ki)/tot_u;                                                          % number of time-dependent parameters
for i = 1:tot_u
    xi = [x(1:ki,1); x(ki+(i-1)*td+1:ki+i*td,1)];                           % parameters for that specific year = time independent ones & time dependent for that year
    A = Amatrix_time(xi,mdl);                                               % constructing the A matrix
    f = (A(bts(i),:)*(A^(5) + A^(6) + A^(7) + A^(8) + A^(9)))'/5;           % marginal distribution of inflation at t+6~t+10 in the model
    [g5,g10] = avg_inf_510(A,bts(i),pi_bar,pi_lim);                         % distribution of 5y, 10y average inflation in the model
    q = [(f - f_d(:,i)); (g5 - g_d(:,1,i)); (g10 - g_d(:,2,i))];
    Qs(i) = (q'*q/24)^(1/2);
    fg_d = [f_d(:,i); g_d(:,1,i); g_d(:,2,i)];
    SST = sum((fg_d - mean(fg_d)).^2);
    R2(i,:) = [1 - q'*q/SST q'*q SST];
%    R2(i,:) = 1 - [sum((f-f_d(:,i)).^2)/sum((f_d(:,i)-mean(f_d(:,i))).^2) sum((g5-g_d(:,1,i)).^2)/sum((g_d(:,1,i)-mean(g_d(:,1,i))).^2) sum((g10-g_d(:,2,i)).^2)/sum((g_d(:,2,i)-mean(g_d(:,2,i))).^2)]; % can't do it separately for each of the 3 sets of moments because it can be > 1 for a given moment
end
Q = sum(Qs);                                                                % objective function: sum of root MSEs for each period
end
